package com.softhis.rekrutacja.client.presenter;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Widget;
import com.mvp4g.client.annotation.Presenter;
import com.mvp4g.client.presenter.BasePresenter;
import com.softhis.rekrutacja.client.MainEventBus;
import com.softhis.rekrutacja.client.commons.ClientSession;
import com.softhis.rekrutacja.client.view.MainView;
import com.softhis.rekrutacja.client.view.widgets.interfaces.IButton;

/**
 * 
 * @author Sasak
 */
@Presenter(view = MainView.class)
public class MainPresenter extends BasePresenter<MainPresenter.MainViewInterface, MainEventBus> {


	@Override
	public void bind() {

		view.getProfileB().addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {
				// userService.isSessionValid(ClientSession.getSessionId(), new
				// AsyncCallback<String>() {
				//
				// public void onFailure(Throwable caught) {
				// Log.debug("[MAIN PRESENTER] is session valid failed",
				// caught);
				// MessageBox.alert("Komunikat",
				// "Utracono połącznie z serwerem, spróbuj ponownie później",
				// null);
				// }
				//
				// public void onSuccess(String result) {
				// Log.debug("[MAIN PRESENTER] received token=" + result);
				// if (result != null) {
				// ClientSession.setSessionId(result);
				// eventBus.openProfile();
				// } else {
				// Log.error("MAIN PRESENTER] not logged, go to login");
				// eventBus.setLogin();
				// }
				// }
				// });
				
				if(ClientSession.getLoggedUser() != null){
					view.setMainHeader("Twoje Konto");
					eventBus.openProfile();
					}
				else{
					view.setMainHeader("Musisz się zalogować");
					eventBus.setLogin();
				}
			}
		});

		view.getMainPageB().addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {
					view.setMainHeader("Strona Główna");
					eventBus.openMainPage();
			}
		});

		view.getNewUserB().addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {
				view.setMainHeader("Stwórz nowe konto");
				eventBus.openNewUser();
			}
		});
		view.getUsersModuleB().addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {
				if(ClientSession.getLoggedUser() != null){
					view.setMainHeader("Moduł edycji użytkowników");
					eventBus.openUsersModule();
					}
				else{
					eventBus.setMainHeader("Musisz się zalogować");
					eventBus.setLogin();
				}
			}
		});

		view.getLogoutB().addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {
				view.setMainHeader("Zostałeś wylogowany");
				ClientSession.setLoggedUser(null);
				eventBus.setLogin();
			}
		});

		view.getLoginB().addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {
				eventBus.setLogin();
				view.setMainHeader("Nie jesteś zalogowany");
			}
		});
	}

	/**
	 * Main init event - checking session
	 */
	public void onInit() {
		eventBus.setLogin();

		// userService.refreshSession(new
		// AsyncCallback<SecureResponse<LoginBean>>() {
		// public void onFailure(Throwable caught) {
		// Log.error("[CORE PRESENTER] session refresh error", caught);
		// eventBus.setLogin();
		// }
		//
		// public void onSuccess(SecureResponse<LoginBean> result) {
		// if (SecureResponseHandler.isSessionValid(result)) {
		// ClientSession.setLoggedUser(result.getResponsePayload().getLoggedUser());
		// }
		// eventBus.setLogged();
		// }
		// });
	}

	public void onSetLogin() {
		view.setLoggedIn(false);
	}

	public void onSetLogged() {
		view.setMainHeader("Zalogowano jako: "+ClientSession.getLoggedUser().getLogin());
		view.setLoggedIn(true);
	}

	public void onSetMainHeader(String header) {
		view.setMainHeader(header);
	}

	/**
	 * Event handler for setContent. Clears the center section and add specified
	 * widget to center section
	 * 
	 * @param content
	 *            content to be added to center section
	 */
	public void onSetContent(Widget content) {
		view.maskCenterSection();
		view.setCenterSection(content);
		view.unmaskCenterSection();
	}

	/**
	 * Main View Interface
	 * 
	 */
	public interface MainViewInterface {

		/**
		 * Used to switch views on the main page
		 * 
		 * @param widget
		 *            widget to be set in the center section
		 */
		public void setCenterSection(Widget widget);

		/**
		 * Masks center section
		 */
		public void maskCenterSection();

		/**
		 * Unmasks center section
		 */
		public void unmaskCenterSection();

		/**
		 * Sets header on the center section
		 * 
		 * @param message
		 */
		public void setMainHeader(String header);

		/**
		 * Sets visibility of menu buttons
		 */
		public void setLoggedIn(boolean logged);

		/**
		 * @return logout menu button
		 */
		public IButton getLogoutB();

		/**
		 * @return login menu button
		 */
		public IButton getLoginB();

		/**
		 * @return profile menu button
		 */
		public IButton getProfileB();

		/**
		 * @return users menu button
		 */
		public IButton getUsersModuleB();

		/**
		 * @return main page menu button
		 */
		public IButton getMainPageB();

		/**
		 * @return new user menu button
		 */
		public IButton getNewUserB();

	}
}
